<?php
namespace Auth\Model;

use Auth\Model\AbstractModel\GeneralAbstract;

/**
 * @method int getId()
 */
class Users extends GeneralAbstract
{
    public $id;
    public $email;
    public $password;
    public $password_salt;
    public $fullname;
    public $avatar;
    public $phone;
    public $description;
    public $sex;
    public $birthday;
    public $created_time;
    public $modified_time;
    public $last_logined;
    public $creator_id;
    public $modifier_id;
    public $is_active;
    public $is_deleted;
    public $token;
    public $token_time;
    public $balance;
    public $type;
    public $pro_expired_date;
    
    /**
     * @return \Auth\Model\UsersTable
     */
    public function getTable(){
        if(!$this->table)
            $this->table = $this->getServiceLocator()->get("Auth\Model\UsersTable");
        return $this->table;
    }

    public function exchangeArray($data){
        $this->id = (isset($data['id'])) ? $data['id'] : null;
        $this->email = (isset($data['email'])) ? $data['email'] : null;
        $this->password = (isset($data['password'])) ? $data['password'] : null;
        $this->password_salt = (isset($data['password_salt'])) ? $data['password_salt'] : null;
        $this->fullname = (isset($data['fullname'])) ? $data['fullname'] : null;
        $this->avatar = (isset($data['avatar'])) ? $data['avatar'] : null;
        $this->phone = (isset($data['phone'])) ? $data['phone'] : null;
        $this->description = (isset($data['description'])) ? $data['description'] : null;
        $this->sex = (isset($data['sex'])) ? $data['sex'] : null;
        $this->birthday = (isset($data['birthday'])) ? $data['birthday'] : null;
        $this->created_time = (isset($data['created_time'])) ? $data['created_time'] : null;
        $this->modified_time = (isset($data['modified_time'])) ? $data['modified_time'] : null;
        $this->last_logined = (isset($data['last_logined'])) ? $data['last_logined'] : null;
        $this->creator_id = (isset($data['creator_id'])) ? $data['creator_id'] : null;
        $this->modifier_id = (isset($data['modifier_id'])) ? $data['modifier_id'] : null;
        $this->is_active = (isset($data['is_active'])) ? $data['is_active'] : null;
        $this->is_deleted = (isset($data['is_deleted'])) ? $data['is_deleted'] : null;
        $this->token = (isset($data['token'])) ? $data['token'] : null;
        $this->token_time = (isset($data['token_time'])) ? $data['token_time'] : null;
        $this->balance = (isset($data['balance'])) ? $data['balance'] : null;
        $this->type = (isset($data['type'])) ? $data['type'] : null;
        $this->pro_expired_date = (isset($data['pro_expired_date'])) ? $data['pro_expired_date'] : null;
        return $this;
    }

    public function getData(){
        $data = parent::getData();
        return $data;
    }
    public function setData($data){
        return $this->exchangeArray($data);
    }
    public function save(){
        $userTbl = $this->getTable();
        $userData = $this->getData();
        $result = $userTbl->save($userData);
        return $result;
    }
    public function active($emailOrId){
    	return $this->getTable()->activeUser($emailOrId);
    }
    public function getFirstName(){
    	return $this->getNamePart("firstname");
    }
    public function getLastName(){
    	return $this->getNamePart("lastname");
    }
    public function getToken() {
    	return $this->token;
    }
    private function getNamePart($part){
    	if($this->fullname){
    		$arrName = explode(" ", $this->fullname);
    		if(count($arrName) >= 2){
    			if($part == "firstname")
    				return $arrName[0];
    			if($part == "lastname"){
    				unset($arrName[0]);
    				return implode(" ", $arrName);
    			}
    		}else 
    			return $arrName[0];
    	}
    	return false;
    }
    /**
     * @return boolean
     */
    public function isExist($email = null){
    	$email = $email == null ? $this->email : $email;
    	if($email != null)
    		return $this->getTable()->isExist($email);
    	return false;
    }
    public function load($userEmailOrId = null){ 
	        $table = $this->getTable();
	        if($userEmailOrId == null)
	        	$userEmailOrId = $this->id;
	        if($userEmailOrId == null)
	        	$userEmailOrId = $this->email;
	        $user = $table->getUser($userEmailOrId, true);
// 	        unset($user['data']['password']);
// 	        unset($user['data']['password_salt']);
	        if(isset($user))
	        	return $user;
    }
}